package com.gjy.concurrency.juc;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.*;

/**
 * @author 宫静雨
 * @version 1.0
 * @since 2023-04-20 16:08:12
 */
public class Juc1 {

    private static final List<Integer> list = new ArrayList<>();
    private static final ExecutorService pool = Executors.newCachedThreadPool();

    static {
        for (int i = 0; i < 30; i++) {
            list.add(i);
        }
    }

    public static void main(String[] args) throws BrokenBarrierException, InterruptedException {

        CyclicBarrier barrier = new CyclicBarrier(7, () -> {

        });

        for (int i = 0; i < list.size(); i++) {
            pool.execute(() -> {
                System.out.println(Thread.currentThread().getName());
            });
        }

        barrier.await();

    }

    private static void semaphore() {
        Semaphore semaphore = new Semaphore(3);

        for (int i = 0; i < list.size(); i++) {
            pool.execute(() -> {
                try {
                    semaphore.acquire();
                    System.out.println("Thread.currentThread().getName() = " + Thread.currentThread().getName());
                    TimeUnit.SECONDS.sleep(2);
                    semaphore.release();
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
            });
        }
    }
}
